var clearLoanDisabled = function(){
	$("#clear_loan_disabled").attr('disabled','disabled');
	$('#loanBorrower').removeAttr('readonly');
	$('#clientCardId').removeAttr('readonly');
	//$('#clientName').removeAttr('readonly');
	//$('#clientIdCard').removeAttr('readonly');
	$('#_form_loan_default').form('clear');
	//$('#_form_client_default').form('clear');
	$("#clear_loan_disabled").removeAttr('disabled');
};

var clearClientDisabled = function(){
	$("#clear_client_disabled").attr('disabled','disabled');
	//$('#loanBorrower').removeAttr('readonly');
	//$('#clientCardId').removeAttr('readonly');
	$('#clientName').removeAttr('readonly');
	$('#clientIdCard').removeAttr('readonly');
	//$('#_form_loan_default').form('clear');
	$('#_form_client_default').form('clear');
	$("#clear_client_disabled").removeAttr('disabled');
};


var initLoanForm = function(){
	$('#_form_loan_default').form({
		url :'sys/LoanExt/addOrUpdateExt',
		onBeforeLoad : function(param){
			$.messager.progress();
		},
		onLoadSuccess : function(data) {
			//加载选中
			if(_is_not_empty(_param_id)){
				set_loan_disabled();
				}
			$.messager.progress("close");
		},
		onLoadError : function(data) {
			//加载完成之后才构建
			$.messager.progress("close");
			$.dialog.alert("加载内容失败,请重新刷新页面/联系管理员");
		},
		onSubmit : function(param) {
			if (!$(this).form("validate")) {
				//$.messager.alert('提示', "提交的选项中,有错误内容,请检查", "error");
				$.dialog.alert("提交的选项中,有错误内容,请检查");
				return false;
			}
			$.messager.progress();
		},
		success : function(data) {	
			$.messager.progress("close");
			if(_is_empty(data)){
				//$.messager.alert('提示', "未知异常", "error");
				$.dialog.alert("未知异常");
				return false;
			}
			var jd = eval('(' + data + ')');
			if(!!!jd){
				$.dialog.alert("未知异常");
				return false;
			}
			if(jd.state!=200){
				//$.messager.alert('提示', jd.message, "error");
				$.dialog.alert(jd.message);
				return false;
			}
			
			$.dialog({
				icon: 'success.gif',
				title: '提示',
			    content: '操作成功！',
			    ok: function(){
			        this.close();
			    },
			    close: function(){
			    	//清除按钮加样式
					$("#clear_loan_disabled").attr('disabled','disabled');
					$("#clear_client_disabled").attr('disabled','disabled');
					$('#valid_info').css('display','none');
					$('#save_loan').removeAttr('disabled');
					//获取返回的loan对象
					var rtnLoan = jd.data;
					//如果是新增 设值父页面_client_id和_client_user和add_param_id
					if(_is_not_empty(rtnLoan) && _is_empty(_param_id)){
						    parent._condition_tab1 = true;
							if(_is_not_empty(rtnLoan.clientId)){
								parent._client_id = rtnLoan.clientId;
							}
							if(_is_not_empty(rtnLoan.id)){
								$('#myLoanId').attr('value',rtnLoan.id);
								parent.add_param_id = rtnLoan.id;
							}
							if(_is_empty(parent._client_user['clientIdCard']) && _is_not_empty(rtnLoan.clientCardId)){
								parent._client_user['clientIdCard'] = rtnLoan.clientCardId;
							}
							
							if(_is_empty(parent._client_user['clientName']) && _is_not_empty(rtnLoan.loanBorrower)){
								parent._client_user['clientName'] = rtnLoan.loanBorrower;
							}
							$('#myClientId').val(rtnLoan.clientId);
					}
					//提交成功以后让身份证和用户名不可编辑
					set_loan_disabled();
					set_client_disabled(parent._client_user);
					parent._set_tab1_success(true);
			    }
			});
			
			
			/*
			$.dialog.alert('操作成功！',function(){
				//清除按钮加样式
				$("#clear_loan_disabled").attr('disabled','disabled');
				$("#clear_client_disabled").attr('disabled','disabled');
				$('#valid_info').css('display','none');
				$('#save_loan').removeAttr('disabled');
				//获取返回的loan对象
				var rtnLoan = jd.data;
				//如果是新增 设值父页面_client_id和_client_user和add_param_id
				if(_is_not_empty(rtnLoan) && _is_empty(_param_id)){
					    parent._condition_tab1 = true;
						if(_is_not_empty(rtnLoan.clientId)){
							parent._client_id = rtnLoan.clientId;
						}
						if(_is_not_empty(rtnLoan.id)){
							$('#myLoanId').attr('value',rtnLoan.id);
							parent.add_param_id = rtnLoan.id;
						}
						if(_is_empty(parent._client_user['clientIdCard']) && _is_not_empty(rtnLoan.clientCardId)){
							parent._client_user['clientIdCard'] = rtnLoan.clientCardId;
						}
						
						if(_is_empty(parent._client_user['clientName']) && _is_not_empty(rtnLoan.loanBorrower)){
							parent._client_user['clientName'] = rtnLoan.loanBorrower;
						}
						$('#myClientId').val(rtnLoan.clientId);
				}
				//提交成功以后让身份证和用户名不可编辑
				set_loan_disabled();
				set_client_disabled(parent._client_user);
				parent._set_tab1_success(true);
			});
			 */
		}
	});
};

var initClientForm = function(){
	$('#_form_client_default').form({
		url :'sys/Client/addOrUpdate',
		onBeforeLoad : function(param){
			$.messager.progress();
		},
		onLoadSuccess : function(data) {
			//加载完成之后才构建
			if(_is_not_empty(clientId)){
			set_client_disabled();
			}
			$.messager.progress("close");
		},
		onLoadError : function(data) {
			//加载完成之后才构建
			$.messager.progress("close");
			$.dialog.alert("加载内容失败,请重新刷新页面/联系管理员");
		},
		onSubmit : function(param) {
			if (!$(this).form("validate")) {
				$.dialog.alert("提交的选项中,有错误内容,请检查");
				return false;
			}
			$.messager.progress();
		},
		success : function(data) {	
			$.messager.progress("close");
			var jd = eval('(' + data + ')');
			if(!!!jd){
				$.dialog.alert("未知异常");
				return false;
			}
			if(jd.state!=200){
				$.dialog.alert(jd.message);
				return false;
			}
			/*
			$.dialog.alert('操作成功！',function(){
				//清除按钮加样式
				$("#clear_loan_disabled").attr('disabled','disabled');
				$("#clear_client_disabled").attr('disabled','disabled');
				$('#valid_info').css('display','none');
				$('#save_client').removeAttr('disabled');
				//
				if(_is_empty(_param_id)){
					parent._condition_tab2 = true;
				}
				if(_is_empty(parent._client_user)){
					parent._client_user = new Object();
				}
				parent._client_user['clientIdCard'] = $('#clientCardId').attr('value');
				parent._client_user['clientName'] =$('#clientName').attr('value');
				set_client_disabled();
				set_loan_disabled(parent._client_user);
				parent._set_tab2_success(false);
			});*/
			
			$.dialog({
				icon: 'success.gif',
				title: '提示',
			    content: '操作成功！',
			    ok: function(){
			        this.close();
			    },
			    close: function(){
			    	//清除按钮加样式
					$("#clear_loan_disabled").attr('disabled','disabled');
					$("#clear_client_disabled").attr('disabled','disabled');
					$('#valid_info').css('display','none');
					$('#save_client').removeAttr('disabled');
					//
					if(_is_empty(_param_id)){
						parent._condition_tab2 = true;
					}
					if(_is_empty(parent._client_user)){
						parent._client_user = new Object();
					}
					parent._client_user['clientIdCard'] = $('#clientCardId').attr('value');
					parent._client_user['clientName'] =$('#clientName').attr('value');
					set_client_disabled();
					set_loan_disabled(parent._client_user);
					parent._set_tab2_success(false);
			    }
			});
			

		}
	});
};


var initAutoComplete = function(){

	$('#loanSourceName').extraAutoComplete({
		 afterSelectedHandler: function(data){//选择数据之后的回调函数
			  $('#loanSourceMan').val(data.id);
	        }
	});

};
var set_loan_disabled = function(user){
	if(_is_not_empty(user)){
		if(_is_not_empty(user.clientName)){
			$('#loanBorrower').val(user.clientName);
		}
		if(_is_not_empty(user.clientIdCard)){
			$('#clientCardId').val(user.clientIdCard);
		}
	}
	$('#loanBorrower').attr('readonly','readonly');
	$('#clientCardId').attr('readonly','readonly');
};

var set_client_disabled = function(user){
	if(_is_not_empty(user)){
		if(_is_not_empty(user.clientName)){
			$('#clientName').val(user.clientName);
		}
		if(_is_not_empty(user.clientIdCard)){
			$('#clientIdCard').val(user.clientIdCard);
		}
	}
	$('#clientName').attr('readonly','readonly');
	$('#clientIdCard').attr('readonly','readonly');
};

var validSensitive = function(obj){
	if (!$("#_valid_form_default").form("validate")) {
		$.dialog.alert("请填写必要的验证信息");
		return false;
	}
	var valid1 = $('#validLoanBorrower').val(),valid2=$('#validClientCardId').val(),valid3=$('#validClientMobile').val();
	if(_is_empty(valid1) && _is_empty(valid2)  && _is_empty(valid3)){
		$.dialog.alert("姓名|身份证|手机号 必须有一项填写");
		return false;
	}
	//验证敏感信息
	$(obj).attr('disabled','disabled');
	//得到查询条件
	var array = $("#_valid_form_default").serializeArray();
	var queryParams = top.array_to_obj(array, 'name', 'value');
	
	$.dialog({
		id:'sensitive_child_dailog',
		title:'敏感信息',
		lock:true,
		width:750,
		height:500,
		content:'url:'+top.basePath+'jsp/loan/valid/validSensitive.jsp',
		data:queryParams,
		close: function(){
			$(obj).removeAttr('disabled');
	    }
	});
};

var dealSelect = function (info)
{
	console.info(info);
	$('#loanBorrower').val(info.clientName);
	$('#clientCardId').val(info.clientIdCard);
	$('#_form_client_default').form('load',info);
	$('#loanBorrower').attr('readonly','readonly');
	$('#clientCardId').attr('readonly','readonly');
	$('#clientName').attr('readonly','readonly');
	$('#clientIdCard').attr('readonly','readonly');
	
	
};
var validInfo = function(obj){
	if (!$("#_valid_form_default").form("validate")) {
		$.dialog.alert("请填写必要的验证信息");
		return false;
	}
	var valid1 = $('#validLoanBorrower').val(),valid2=$('#validClientCardId').val(),valid3=$('#validClientMobile').val();
	if(_is_empty(valid1) && _is_empty(valid2)  && _is_empty(valid3)){
		$.dialog.alert("姓名|身份证|手机号 必须有一项填写");
		return false;
	}
	//验证敏感信息
	$(obj).attr('disabled','disabled');
	//得到查询条件
	var array = $("#_valid_form_default").serializeArray();
	var queryParams = top.array_to_obj(array, 'name', 'value');
	$.dialog({
		id:'info_child_dailog',
		title:'用户基本信息',
		lock:true,
		width:750,
		height:500,
		content:'url:'+top.basePath+'jsp/loan/valid/validInfo.jsp',
		data:queryParams,
		close: function(){
			//console.info(frameElement.api.opener.data);
			$(obj).removeAttr('disabled');
	    }
	});
};

var init_select = function(){
	
	$('#loanSource').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Source_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	//	source:'server',
		//requestData:{dataBaseCode:'Loan_Source_Code'}
	});
	
	$('#loanType').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Type_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanProduct').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Product_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanNatureBusiness').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Nature_Business_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanNatureClilent').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Nature_Clilent_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanNatureFund').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Nature_Fund_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanPurpose').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Purpose_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanRepayType').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Repay_Type_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	//
	$('#loanCapitalRound').mSelect({
		myClass:'easyui-validatebox',
		width:90,
		baseCode:'Loan_Capital_Round_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanInterestRound').mSelect({
		myClass:'easyui-validatebox',
		width:90,
		baseCode:'Loan_Interest_Round_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanInterestType').mSelect({
		myClass:'easyui-validatebox',
		width:90,
		baseCode:'Loan_Interest_Type_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanObject').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Object_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	$('#webId').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Loan_Web_Id_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#loanKnowStatus').mSelect({
		myClass:'easyui-validatebox',
		width:60,
		baseCode:'Loan_Know_Status_Code',
		isAllSelect:false,
		dataOptions:'required:true'
	});
	
	$('#clientSex').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Client_Sex_Code',
		dataOptions:'required:true'
	//	source:'server',
		//requestData:{dataBaseCode:'Loan_Source_Code'}
	});
	
	$('#clientMaritalStatus').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Client_Marital_Status_Code'
	});
	
	$('#clientEducation').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Client_Education_Code'
	});
	
	$('#clientInsureArea').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Client_Insure_Area_Code'
	});
	
	$('#clientHouseType').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Client_Residence_Type_Code'
	});
	
	$('#clientHouseMember').mSelect({
		myClass:'easyui-validatebox',
		width:308,
		baseCode:'Client_House_Member_Code'
	});
	
};


var saveLoan = function(obj){
	//$("#save_loan").attr('disabled','disabled');
	$('#_form_loan_default').submit();
};

var saveClient = function(obj){
	//$("#save_client").attr('disabled','disabled');
	$('#_form_client_default').submit();
};

$(function(){
	initLoanForm();
	initClientForm();
	//加载下拉框
	init_select();
	initAutoComplete();
	if(_is_not_empty(_param_id)){
		//编辑 我们不用管
		$('#valid_info').css('display','none');
		$("#_form_loan_default").form('load', 'sys/Loan/findByPk?pks=' + _param_id);
		$("#clear_loan_disabled").attr('disabled','disabled');
		$("#clear_client_disabled").attr('disabled','disabled');
		$('#clear_loan_disabled').css('display','none');
		$('#clear_client_disabled').css('display','none');
	}else{
		$('#valid_info').css('display','block');
	}
	
	if(_is_not_empty(clientId)){
		//编辑 我们不用管
		//$('#valid_info').css('display','none');
		$("#_form_client_default").form('load', 'sys/Client/findByPk?pks=' + clientId);
	//	$("#clear_loan_disabled").attr('disabled','disabled');
		//$("#clear_client_disabled").attr('disabled','disabled');
	}
	
	$('input[name="confirm_button"]').click(function(){
		$('#_form_default').submit();
	});
	
});
